package com.xujl.mvc;

import cn.hutool.core.collection.CollectionUtil;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.DataKeys;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.CollectionComboBoxModel;
import com.xujl.mvc.entity.ArgsEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;

public class TestUI extends JDialog {
    Logger logger = LoggerFactory.getLogger(TestUI.class);
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField className;
    private JComboBox flieTypeView;
    private JComboBox language;
    private JLabel classnameLable;
    private JLabel fileType;
    private Project project;
    private ArgsEntity argsEntity = new ArgsEntity();

    public static String[] fileTypeData = new String[]{"Activity", "Fragment"};

    public TestUI(AnActionEvent e) {
        this.project = e.getProject();
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(e12 -> onOK());

        buttonCancel.addActionListener(e13 -> onCancel());

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(e14 -> onCancel(), KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);


        flieTypeView.setModel(new CollectionComboBoxModel(CollectionUtil.toList(fileTypeData)));

        ArrayList<String> strings = new ArrayList<>();

        strings.add("JAVA");
        strings.add("KOTLIN");
        language.setModel(new CollectionComboBoxModel(strings));


        // 项目根路径
        String rootPath = project.getBasePath();

        argsEntity.setRootPath(rootPath);

        VirtualFile selectGroup = DataKeys.VIRTUAL_FILE.getData(e.getDataContext());
        Module module = DataKeys.MODULE.getData(e.getDataContext());

        argsEntity.setModulename(module.getName().substring(module.getName().indexOf(".") + 1));
        String modulename = argsEntity.getModulename();

        //点选的目录
        String selectpath = selectGroup.getPath();
        argsEntity.setSelectpath(selectpath);

        // 当前module 根路径 排除包名的
        String moudleroot = rootPath + "/" + modulename + "/src/main/java/";
        argsEntity.setMoudleroot(moudleroot);

        String currentSelectPackage = selectpath.substring(selectpath.indexOf("/java") + 6).replaceAll("/", ".");
        argsEntity.setPagePath(currentSelectPackage);

        argsEntity.setReslayout(rootPath + "/" + modulename + "/src/main/res/layout");

        flieTypeView.addItemListener(e1 -> {
            if ("Activity".equals(flieTypeView.getSelectedItem())) {
                argsEntity.setType(0);
//                    argsEntity.setReslayout(rootPath + "/" + modulename + "/src/main/res/layout");
            } else if ("Fragment".equals(flieTypeView.getSelectedItem())) {
//                    argsEntity.setReslayout(rootPath + "/" + modulename + "/src/main/res/layout");
                argsEntity.setType(1);
            }
        });

    }

    private void onOK() {
        AutoFtl autoFtl = new AutoFtl(argsEntity);
        logger.error(argsEntity.toString());
        argsEntity.setIskt(language.getSelectedItem().equals("KOTLIN"));
        argsEntity.setName(className.getText());
        if (flieTypeView.getSelectedItem().equals("Activity")) {
            argsEntity.setClassName(className.getText() + "Activity");
        } else if (flieTypeView.getSelectedItem().equals("Fragment")) {
            argsEntity.setClassName(className.getText() + "Fragment");
        }
        autoFtl.GeneratedView();
        autoFtl.GeneratedModel();
        autoFtl.GeneratedControl();
//        Messages.showMessageDialog(project, argsEntity.toString(), "Information", Messages.getInformationIcon());
        autoFtl.GeneratedXml();
        project.getBaseDir().refresh(false, true);
        dispose();
    }

    private void onCancel() {
        dispose();
    }


}
